clear all
set more off
use "/Users/yasenov/Desktop/replication package/master.dta"

**************
**************
**************

*** GET SD STARTTIME TERCILES ***
preserve
collapse SDstarttime, by(student_id_group)
xtile SDstarttime_median = SDstarttime, n(2)
drop SDstarttime
tempfile edno
save `edno'
restore

merge m:1 student_id_group using `edno', nogen
/*				*/

**************
**************

local controls student_fatigue
local cluster cluster(student_id_group)
local absorb absorb(classroom_group student_id_group)

la var starttime "\ \ \ Start Time"
la var SDstarttime "\ \ \ $ \sigma $(Start Time)"
la var SDstarttime9am "\ \ \ $ \sigma $(Start Time)"
la var IQRstarttime "\ \ \ IQR(Start Time)" 

eststo clear
eststo: reghdfe receiveZero starttime SDstarttime `controls', `absorb' `cluster'
eststo: reghdfe receiveZero starttime SDstarttime `controls' if period_start<12.5, `absorb' `cluster'
eststo: reghdfe receiveZero starttime SDstarttime `controls' if male==1, `absorb' `cluster'
eststo: reghdfe receiveZero starttime SDstarttime `controls' if prescore <= 14, `absorb' `cluster'
eststo: reghdfe receiveZero starttime SDstarttime `controls' if SDstarttime_median==2, `absorb' `cluster'
eststo: reghdfe receiveZero starttime SDstarttime `controls' if stem==1, `absorb' `cluster'
preserve
drop SDstarttime
rename SDstarttime9am SDstarttime
eststo: reghdfe receiveZero starttime SDstarttime `controls', `absorb' `cluster'	
restore
eststo: reghdfe receiveZero starttime SDstarttime `controls', `absorb' cluster(student_id_group classroom_group)

quietly esttab using "MainResults-Subsample.tex", ///
    replace se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes ///
	refcat(starttime "\underline{Outcome: Received zero}", nolabel) ///
	keep(starttime SDstarttime) order(starttime SDstarttime) ///
	noconstant nogaps ///
	mgroups("" "Subsample" "", pattern(0 1 0 0 0 0 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  ///
    mtitles("\begin{tabular}{@{}c@{}}Full \\ sample \end{tabular}" "\begin{tabular}{@{}c@{}}Morning \\ classes \end{tabular}" "\begin{tabular}{@{}c@{}}Male \\ students \end{tabular}" "\begin{tabular}{@{}c@{}} Low \\ achievers \end{tabular}" "\begin{tabular}{@{}c@{}}High Schedule \\ Volatility \end{tabular}" "\begin{tabular}{@{}c@{}} STEM \\ courses \end{tabular}" "\begin{tabular}{@{}c@{}}9am top \\ coding \end{tabular}" "\begin{tabular}{@{}c@{}}Two-way \\ Clustering \end{tabular}") label postfoot("")

**************
**************

eststo clear
eststo: reghdfe grade starttime SDstarttime `controls', `absorb' `cluster'
eststo: reghdfe grade starttime SDstarttime `controls' if period_start<12.5, `absorb' `cluster'
eststo: reghdfe grade starttime SDstarttime `controls' if male==1, `absorb' `cluster'
eststo: reghdfe grade starttime SDstarttime `controls' if prescore <= 14, `absorb' `cluster'
eststo: reghdfe grade starttime SDstarttime `controls' if SDstarttime_median==2, `absorb' `cluster'
eststo: reghdfe grade starttime SDstarttime `controls' if stem==1, `absorb' `cluster'
preserve
drop SDstarttime
rename SDstarttime9am SDstarttime
eststo: reghdfe grade starttime SDstarttime `controls', `absorb' `cluster'	
restore
eststo: reghdfe grade starttime SDstarttime `controls', `absorb' cluster(student_id_group classroom_group)

quietly esttab using "MainResults-Subsample.tex", ///
    append se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes ///
	keep(starttime SDstarttime) order(starttime SDstarttime) ///
	refcat(starttime "\underline{Outcome: Raw grade [1,10]}", nolabel) noconstant nogaps ///
   mtitles("" "" "" "" "" "" "" "") label postfoot("") prehead("") 

**************
**************

eststo clear
eststo: reghdfe zGradeConditional starttime SDstarttime `controls', `absorb' `cluster'
estadd local classroomFE "X"
estadd local studentFE "X"
eststo: reghdfe zGradeConditional starttime SDstarttime `controls' if period_start<12.5, `absorb' `cluster'
estadd local classroomFE "X"
estadd local studentFE "X"
eststo: reghdfe zGradeConditional starttime SDstarttime `controls' if male==1, `absorb' `cluster'
estadd local classroomFE "X"
estadd local studentFE "X"
eststo: reghdfe zGradeConditional starttime SDstarttime `controls' if prescore <= 14, `absorb' `cluster'
estadd local classroomFE "X"
estadd local studentFE "X"
eststo: reghdfe zGradeConditional starttime SDstarttime `controls' if SDstarttime_median==2, `absorb' `cluster'
estadd local classroomFE "X"
estadd local studentFE "X"
eststo: reghdfe zGradeConditional starttime SDstarttime `controls' if stem==1, `absorb' `cluster'
estadd local classroomFE "X"
estadd local studentFE "X"
preserve
drop SDstarttime
rename SDstarttime9am SDstarttime
eststo: reghdfe zGradeConditional starttime SDstarttime `controls', `absorb' `cluster'	
estadd local classroomFE "X"
estadd local studentFE "X"
restore
eststo: reghdfe zGradeConditional starttime SDstarttime `controls', `absorb' cluster(student_id_group classroom_group)
estadd local classroomFE "X"
estadd local studentFE "X"


quietly esttab using "MainResults-Subsample.tex", ///
	append se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes ///
	keep(starttime SDstarttime) order(starttime SDstarttime) ///
	refcat(starttime "\underline{Outcome: Standardized grade}", nolabel) noconstant nogaps ///
	scalars("classroomFE Classroom FE" "studentFE Student FE") ///
   mtitles("" "" "" "" "" "" "" "") label prehead("") 
